package com.sxfq.loansupermarket.backsystem.modular.report.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IOutpatientManagementService;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.MarketReportInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.SearchMarketReportInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.service.IMarketReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/marketReport")
public class marketReportController extends BaseController {
    @Autowired
    IMarketReportService service;

    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    IOutpatientManagementService outpatientManagementService;

    private String PREFIX = "/report/wholeHospital/";

    /**
     * 跳转到市场全院报表页面
     */
    @RequestMapping("")
    public String to_marketReport(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getSessionAttr("dimensionList") == null) {
            String dimensionsql = "select id, dimension_china_name as dimensionChinaName," +
                    " dimension_english_name as dimensionEnglishName from market_dimension_info order by num;";
            List<Map<String, Object>> dimensionList=sqlMapper.selectList(dimensionsql);
            ShiroKit.setSessionAttr("dimensionList", dimensionList);
        }
        List<Map<String, Object>> outpatientManagementList = outpatientManagementService.lists(ShiroKit.getUser().getAccount());
        model.addAttribute("outpatientManagementList", outpatientManagementList);
        model.addAttribute("arrivalStoreValue", ShiroKit.getUser().getOutpatientName());
        model.addAttribute("dimensionList", ShiroKit.getSessionAttr("dimensionList"));
        model.addAttribute("sttleAccountsTime", ShiroKit.getUser().getSttleAccountsTime());
        return PREFIX + "marketReport.html";
    }

    /**
     * 跳转市场全院报表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(SearchMarketReportInfo searchInfo, HttpServletRequest request) {
        Page<MarketReportInfo> page = new PageFactory<MarketReportInfo>().defaultPage();
        try {
            List<MarketReportInfo> list = new ArrayList<MarketReportInfo>();
            if ("personnel".equals(searchInfo.getDimensionSelection())) {  //市场人员
                list = service.getMarketPersonnel(searchInfo);
            } else if ("channel".equals(searchInfo.getDimensionSelection())) {  //市场渠道
                list = service.getChannelList(searchInfo);
            }
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }
}